package dk.eclipse.android.helpers;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.FloatMath;

/**
 * This helper handles GPS typical operations.
 * 
 * @author Ronni Kahalani
 * 
 */
public class GPSHelper {

	/**
	 * Sets a location listener to retrieve location updates.
	 * 
	 * @param context
	 * @param listener
	 */
	public static void listenToLocation(Context context,
			LocationListener listener) {
		/* Use the LocationManager class to obtain GPS locations */
		LocationManager locManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				listener);
	}

	/**
	 * Returns the last known location.
	 * 
	 * @param context
	 * @param listener
	 * @return the last known location
	 */
	public static Location getLastKnownLocation(Context context) {
		/* Use the LocationManager class to obtain GPS locations */
		LocationManager locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		return locationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
	}

	/**
	 * Returns the last known latitude location as a double.
	 * 
	 * @param context
	 * @param listener
	 * @return the last known latitude location
	 */
	public static Double getLastKnownLatitude(Context context) {
		Location location = getLastKnownLocation(context);
		if (location != null) {
			return Double.valueOf((location.getLatitude()));
		}
		return null;
	}

	/**
	 * Returns the last known longitude location as a double.
	 * 
	 * @param context
	 * @param listener
	 * @return the last known longitude location
	 */
	public static Double getLastKnownLongitude(Context context) {
		Location location = getLastKnownLocation(context);
		if (location != null) {
			return Double.valueOf(location.getLongitude());
		}
		return null;
	}

	/**
	 * Returns the last known latitude location as an int.
	 * 
	 * @param context
	 * @param listener
	 * @return the last known latitude location
	 */
	public static int getLastKnownLatitudeAsInt(Context context) {
		return (int) (getLastKnownLatitude(context).longValue());
	}

	/**
	 * Returns the last known longitude location as an int.
	 * 
	 * @param context
	 * @param listener
	 * @return the last known longitude location
	 */
	public static int getLastKnownLongitudeAsInt(Context context) {
		return (int) (getLastKnownLongitude(context).longValue());
	}

	/**
	 * Returns the distance between two GPS locations.
	 * 
	 * @param lat_a
	 * @param lng_a
	 * @param lat_b
	 * @param lng_b
	 * @return the distance in meters
	 */
	public static double gps2m(float lat_a, float lng_a, float lat_b,
			float lng_b) {
		float pk = (float) (180 / 3.14169);

		float a1 = lat_a / pk;
		float a2 = lng_a / pk;
		float b1 = lat_b / pk;
		float b2 = lng_b / pk;

		float t1 = FloatMath.cos(a1) * FloatMath.cos(a2) * FloatMath.cos(b1)
				* FloatMath.cos(b2);
		float t2 = FloatMath.cos(a1) * FloatMath.sin(a2) * FloatMath.cos(b1)
				* FloatMath.sin(b2);
		float t3 = FloatMath.sin(a1) * FloatMath.sin(b1);
		double tt = Math.acos(t1 + t2 + t3);

		return 6366000 * tt;
	}

}
